<?php
/**
 * panx后台管理员，查看设备账号的使用状况
 * Author: lyun
 * Date: 2019/3/14
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Panx;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\DeviceAccount\DeviceAccountUpdateRequest;
use App\Models\Admin\PanxAdmin;
use App\Models\Customer\Customer;
use App\Models\Customer\DeviceAccount;
use App\Models\Store\Store;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\DB;

class DeviceAccountController extends ApiController
{
    /**
     * panx后台，查看设备账号列表
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(Request $request)
    {
        // 超管才能操作的
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        /** @var LengthAwarePaginator|Collection $device_accounts */
        $device_accounts = (new DeviceAccount)
            ->select([
                'id', 'account', 'enabled', 'alive_seconds', 'created_at', 'customer_id', 'store_id', 'location',
                DB::raw('(SELECT COUNT(*) AS total FROM records WHERE records.recorderable_id = device_accounts.id) AS product_try_on_count'),
                DB::raw('(SELECT COUNT(*) AS skin_record_count FROM skin_records WHERE skin_records.device_account_id = device_accounts.id) AS skin_record_count')
            ])
            ->filterAccountKeyword($request->account_keyword)
            ->filterEnabled($request->enabled)
            ->filterStoreId($request->store_id)
            ->filterCustomerId($request->customer_id)
            ->OrderBys($request->sort)
            ->with(['customer', 'store'])
            ->latest('id')
            ->paginate($request->per_page ?? 10);

        $this->transformDeviceAccounts($device_accounts);

        return $this->response(compact('device_accounts'));
    }

    /**
     *  返回客户和店铺信息列表
     *
     * @return array|\Illuminate\Http\Response
     */
    public function create()
    {
        $customers = (new Customer)->get();
        $stores = (new Store)->get();

        return $this->response(compact('customers', 'stores'));
    }

    /**
     * panx后台，更新设备账号状态
     *
     * @param DeviceAccountUpdateRequest $request
     * @param $device_account_id
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(DeviceAccountUpdateRequest $request, $device_account_id)
    {
        // 超管才能操作的
        $this->checkIsTrue(PanxAdmin::checkIsSuperMan(), trans('admin.is_super_man'));

        $device_account = (new DeviceAccount)->find($device_account_id);
            $this->checkItemExist($device_account);

        $enabled = $request->enabled;

        $updated = $device_account->update(['enabled' => $enabled]);

        return $this->response(compact('updated'));
    }

    /**
     * @param LengthAwarePaginator $device_accounts
     */
    private function transformDeviceAccounts(LengthAwarePaginator $device_accounts)
    {
        $device_accounts->transform(function(DeviceAccount $device_account){
            return [
                'id'                   => $device_account->id,
                'account'              => $device_account->account,
                'store'                => $device_account->store->name ?? null,
                'customer'             => $device_account->customer->name ?? null,
                'location'             => $device_account->location,
                'enabled'              => $device_account->enabled,
                'product_try_on_count' => $device_account->product_try_on_count,
                'skin_record_count'    => $device_account->skin_record_count,
                'alive_seconds'        => $device_account->alive_seconds,
                'created_at'           => (string)$device_account->created_at,
            ];
        });
    }
}
